package com.yyl.fmall.order.controller;


import com.yyl.fmall.order.entity.Orders;
import com.yyl.fmall.order.service.IOrdersService;
import com.yyl.fmall.order.vo.AddOrderReturnVo;
import com.yyl.fmall.utils.CommonConstants;
import com.yyl.fmall.utils.R;
import com.yyl.fmall.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单  前端控制器
 * </p>
 *
 * @author YuanSir
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrdersController {
    
    @Autowired
    @Qualifier("ordersTeacherServiceImpl")
    private IOrdersService ordersService;


    @PostMapping("/add")
    public R add(String cids,@RequestBody Orders orders) throws Exception {
    
        AddOrderReturnVo addOrderReturnVo = ordersService.add(cids, orders);
        
        return R.ok().put(CommonConstants.R_MAP_KEY, addOrderReturnVo);
    
    }
    
    
    @GetMapping("/status/{orderId}")
    public R status(@PathVariable("orderId") String orderId) throws Exception {
        
        String status = ordersService.status(orderId);
        
        return R.ok().put(CommonConstants.R_MAP_KEY, status);
        
    }
    
    
    /**
     * 模拟微信支付回调接口
     * @param orderId
     * @return
     */
    @GetMapping("/callBack")
    public R payCallBack(String orderId){
        
        //当接口被调用时就认为订单支付成功了
        WebSocketServer.sendMsg(orderId, "1");
        
        return R.ok();
        
    }

}
